package com.example.ai.controller;

import com.example.ai.dto.PasswordUpdateDTO;
import com.example.ai.dto.UserProfileDTO;
import com.example.ai.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/profile")
    public ResponseEntity<UserProfileDTO> getUserProfile(Authentication authentication) {
        String username = authentication.getName();
        return ResponseEntity.ok(userService.getUserProfile(username));
    }

    @PutMapping("/profile")
    public ResponseEntity<UserProfileDTO> updateUserProfile(
            Authentication authentication,
            @RequestBody UserProfileDTO profileDTO) {
        String username = authentication.getName();
        return ResponseEntity.ok(userService.updateUserProfile(username, profileDTO));
    }

    @PutMapping("/password")
    public ResponseEntity<Void> updatePassword(
            Authentication authentication,
            @RequestBody PasswordUpdateDTO passwordDTO) {
        String username = authentication.getName();
        userService.updatePassword(username, passwordDTO);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/avatar")
    public ResponseEntity<String> uploadAvatar(
            Authentication authentication,
            @RequestParam("file") MultipartFile file) {
        String username = authentication.getName();
        String avatarUrl = userService.uploadAvatar(username, file);
        return ResponseEntity.ok(avatarUrl);
    }
} 